package com.shiro.app;

import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.mgt.DefaultSessionStorageEvaluator;
import org.apache.shiro.mgt.DefaultSubjectDAO;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;

@Configuration
public class AppShiroConfig {

    @Bean("app_shiro")
    public ShiroFilterFactoryBean shiroFilterFactoryBean(){

        DefaultSecurityManager defaultSecurityManager = new DefaultWebSecurityManager();
        DefaultSubjectDAO defaultSubjectDAO = new DefaultSubjectDAO();
        DefaultSessionStorageEvaluator defaultSessionStorageEvaluator = new DefaultSessionStorageEvaluator();
        defaultSubjectDAO.setSessionStorageEvaluator(defaultSessionStorageEvaluator);
        defaultSecurityManager.setSubjectDAO(defaultSubjectDAO);
        defaultSecurityManager.setRealm(new LoginRealm());

        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(defaultSecurityManager);
        shiroFilterFactoryBean.setLoginUrl("index.html");
        LinkedHashMap filterMap = new LinkedHashMap();
        //filterMap.put("anon", new AnonymousFilter());
        //filterMap.put("jwt",new JwtFilter(applicationContext));
        filterMap.put("app",new JWTFilter());
        shiroFilterFactoryBean.setFilters(filterMap);
        LinkedHashMap<String,String> fiterRealmMap = new LinkedHashMap<>();
        fiterRealmMap.put("/app/v1/login" , "anon") ;
        fiterRealmMap.put("/app/v1/regist" , "anon") ;
        fiterRealmMap.put("/app/v1/sendEmail" , "anon") ;
        fiterRealmMap.put("/app/v1/**" , "anon") ;
        fiterRealmMap.put("/app/app_guojia/**" , "anon") ;
        //fiterRealmMap.put("/app/v1/notice/**" , "app,authc") ;
        //fiterRealmMap.put("/app/v1/**/**" , "anon") ;
        fiterRealmMap.put("/app/v1/apk/apkUpdate" , "anon") ;
        fiterRealmMap.put("/app/v1/upgrade/**" , "anon") ;
        fiterRealmMap.put("/app/v1/**/**" , "app,authc") ;

        //fiterRealmMap.put("/**","jwt,authc");
        //shiroFilterFactoryBean.setSuccessUrl("/admin/main.html");



        //shiroFilterFactoryBean.setLoginUrl("/index.html");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(fiterRealmMap);
        return shiroFilterFactoryBean;
    }
}
